package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayStore;
import org.itboys.mobile.service.mongo.web.SundayStoreService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangyang on 2017/12/27.
 * 圣代_实体店_后台_controller
 */
@Controller
@RequestMapping("/sunday/web/store")
public class SundayStoreController extends BaseController{

    @Autowired
    private SundayStoreService physicalStoreService;

    /**
     * 实体店主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,Model model){

        return "/sunday/store/index";
    }

    /**
     * 获取数据
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response
    ) {
        try {
            Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request, "name");
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("isDeleted",0);
            PageResult<SundayStore> result = physicalStoreService.containpageQuery(request,containsparam, param);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/web/store/select------"+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }

    }

    /**
     * 新增和修改页面
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(HttpServletRequest request,
            @RequestParam(value="id",required=true) Long id,Model model){
        SundayStore store = new SundayStore();
        if(id == 0){
            store.setId(0L);
        }else{
            store = physicalStoreService.getById(id);
        }
        model.addAttribute("store",store);
            return "/sunday/store/input";
    }

    /**
     * 保存实体店
     * @param request
     * @param store
     * @param imageFile
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     @ModelAttribute SundayStore store,
                     @RequestParam(value="imageFile",required=false) MultipartFile imageFile){
        try {
            physicalStoreService.save(store,imageFile);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/store/save------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }

    /**
     * 删除
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                        @RequestParam(value="id",required=true) Long id){
        try {
            physicalStoreService.delete(id);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/store/delete------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }
}
